/*global pi, ArrayBuffer */

pi.mod.define('pi.os.util', function (pi) {
	"use strict";
	var indexStartProcess, indexConfirm, indexSystemCmds, indexGetProcessIdByName, indexJSChdir, indexGetCwd, indexGetAppDirStr, indexGetFullPathName, indexGetCursorPos, indexWideCharToMultiByte, module = {},
		dllPath = "TestDll.dll";

	module.MAX_PATH = 260;

	/*
	 * 启动新进程
	 * args:命令行参数
	 */
	indexStartProcess = 0;
	module.start = function (args) {
		if (!indexStartProcess) {
			indexStartProcess = pi.vcall.register(dllPath, "StartProcess", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexStartProcess, args);
	};

	//执行系统/dos命令
	indexSystemCmds = 0;
	module.cmd = function (cmds) {
		if (!indexSystemCmds) {
			indexSystemCmds = pi.vcall.register(dllPath, "SystemCmds", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexSystemCmds, cmds);
	};

	//获取进程ID by name
	indexGetProcessIdByName = 0;
	module.getIdByName = function (pname) {
		if (!indexGetProcessIdByName) {
			indexGetProcessIdByName = pi.vcall.register(dllPath, "GetProcessIdByName", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexGetProcessIdByName, pname);
	};

	//改变当前路径
	indexJSChdir = 0;
	module.setCwd = function (path) {
		if (!indexJSChdir) {
			indexJSChdir = pi.vcall.register(dllPath, "JSChdir", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexJSChdir, path);
	};

	//获取当前路径
	indexGetCwd = 0;
	module.getCwd = function () {
		var s, buf;
		if (!indexGetCwd) {
			indexGetCwd = pi.vcall.register("Kernel32.dll", "GetCurrentDirectoryW", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER]);
		}

		buf = pi.lang.createArrayBuffer(260 * 2);
		pi.vcall.invoke(indexGetCwd, 260, buf);
		s = pi.vcall.readBuffer(buf, pi.vcall.TYPE_STRING);
		return s;
	};

	//获取exe启动文件夹
	indexGetAppDirStr = 0;
	module.getAppDir = function () {
		if (!indexGetAppDirStr) {
			indexGetAppDirStr = pi.vcall.register(dllPath, "JSGetAppDir", pi.vcall.TYPE_STRING);
		}
		return pi.vcall.invoke(indexGetAppDirStr);
	};

	/*
	 * msdn:
	 * DWORD WINAPI GetFullPathName(
	 *	__in          LPCTSTR lpFileName,
	 *	__in          DWORD nBufferLength,
	 *	__out         LPTSTR lpBuffer,
	 *	__out         LPTSTR* lpFilePart
	 * );
	If the function succeeds, the return value is the length of the string copied to lpBuffer, not including the terminating null character, in TCHARs.
	return full path string is success; else undefined
	 */
	indexGetFullPathName = 0;
	module.getFullPathName = function (fileName) {
		var r, buf;
		if (!indexGetFullPathName) {
			indexGetFullPathName = pi.vcall.register("kernel32.dll", "GetFullPathNameW", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING, pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT]);
		}
		buf = pi.lang.createArrayBuffer(module.MAX_PATH * 2);
		r = pi.vcall.invoke(indexGetFullPathName, fileName, module.MAX_PATH, buf, null);
		if (r > 0) {
			return pi.vcall.readBuffer(buf, pi.vcall.TYPE_STRING);
		}
	};

	//暂停
	module.pause = function () {
		module.os.cmd("pause");
	};

	//获取鼠标当前位置
	//bScreen: 是否屏幕坐标 
	indexGetCursorPos = 0;
	module.getCursorPos = function (bScreen) {
		var b, buf;
		if (!indexGetCursorPos) {
			indexGetCursorPos = pi.vcall.register("user32.dll", "GetCursorPos", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_POINTER]);
		}
		buf = pi.lang.createArrayBuffer(8);
		b = pi.vcall.invoke(indexGetCursorPos, buf);
		if(b){
			var screenPos = pi.vcall.createObjectFromBuffer(buf, ["x", pi.vcall.TYPE_INT, "y", pi.vcall.TYPE_INT]);
			if(bScreen){
				return screenPos;
			}
			else{
				if(pi.os.window.screenToClient(pi.vcall.webViewNativeHandle, screenPos)){
					return screenPos;
				}
			}
		}
	};

	//WideCharToMultiByte, see msdn
	indexWideCharToMultiByte = 0;
	module.wideCharToMultiByte = function (codePage, flags, wstr, wstrCount, mbStr, mbStrByte, defChar, usedDefChar) {
		if (!indexWideCharToMultiByte) {
			indexWideCharToMultiByte = pi.vcall.register("kernel32.dll", "WideCharToMultiByte", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_STRING, pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]);
		}
		return pi.vcall.invoke(indexWideCharToMultiByte, codePage, flags, wstr, wstrCount, mbStr, mbStrByte, defChar, usedDefChar);
	};

	//WideCharToMultiByte simple ansi wrapper: 计算转换为ansi编码后的长度, 不包含\0
	module.ansiLength = function (str) {
		var r = module.wideCharToMultiByte(0, 0, str, str.length, null, 0, 0, 0);
		return r;
	};

	return module;
});